﻿using AutoMapper;
using MDT.LifeSewagePlatform.Conmmon;
using MDT.LifeSewagePlatform.InterfaceService;
using MDT.LifeSewagePlatform.Entity;
using MDT.LifeSewagePlatform.IRepository;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MDT.LifeSewagePlatform.Service
{
    public class PlaceDutyService:BaseService,IPlaceDutyService
    {
        #region ctor and prop
        [Autowired]
        private  IPlaceDutyRepository placedutyRepository { get; set; }
        [Autowired]
        private  IMapper mapper{ get; set; }
        public PlaceDutyService()
        {
         
        }
        #endregion

        #region method service
        public async Task<BaseMessage> Add(AddPlaceDuty model,int UserID)
        {
            #region params verify 
            var verifyResualt = model.Verify();
            if (verifyResualt.Item2 == false)
            {
                return BadRequest400(verifyResualt.Item1);
            }
            #endregion

            if (await placedutyRepository.AnyAsync(t=>t.AreaID==model.AreaID&& t.ProvinceID == model.ProvinceID && t.CityID==model.CityID))
            {
                return BadRequest400("添加失败该地区人口数量已经添加!");
            }

            PlaceDutyEntity placeDuty = mapper.Map<PlaceDutyEntity>(model);
            placeDuty.Email = "1111@163.com";
            placeDuty.PhoneNumber = "123";
            placeDuty.DutyName = "111";
            await placedutyRepository.Create(placeDuty,UserID);
            if(await placedutyRepository.SaveChangesAsync())
            {
                return Ok200("添加成功!");
            }
            return BadRequest400("添加失败!");
        }

        public async Task<BaseMessage> Update(UpdatePlaceDuty model, int ID,int UserID)
        {

            #region params verify 
            var verifyResualt = model.Verify();
            if (verifyResualt.Item2 == false)
            {
                return BadRequest400(verifyResualt.Item1);
            }
            #endregion

            PlaceDutyEntity placeDuty = await placedutyRepository.FirstOrDefaultAsync(p => p.IsDelete == false && p.ID==ID) ;
            mapper.Map(model,placeDuty);
            var entity= await placedutyRepository.Update(placeDuty, ID);
            if (await placedutyRepository.SaveChangesAsync())
            {
                return Ok200("更新成功!");
            }
            return BadRequest400("更新失败!");
        }

        public async Task<BaseMessage> GetPlaceDutys(PlaceDutyParameter Params)
        {
            IQueryable<PlaceDutyView> placeDutyViews = placedutyRepository.GetPlaceDutys(t=>true);
            return Ok200("查询成功!",await PageList<PlaceDutyView>.CreateAsync(placeDutyViews, Params));
        }

        public async Task<BaseMessage> Delete(int id)
        {
            await placedutyRepository.Delete(t=>t.ID==id);
            if (await placedutyRepository.SaveChangesAsync())
            {
                return Ok200("删除成功!");
            }
            return BadRequest400("删除失败!");
        }
        #endregion
    }
}
